package bg.btanev.eightyfour.gwtfacade.commandhandler;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.Validate;

import bg.btanev.eightyfour.domain.Target;
import bg.btanev.eightyfour.domain.profile.Profile;
import bg.btanev.eightyfour.domain.profile.ProfileEvent;
import bg.btanev.eightyfour.gwtfacade.converter.DtoConverter;
import bg.btanev.eightyfour.service.ProfileService;
import bg.btanev.eightyfour.service.TargetService;
import bg.btanev.eightyfour.shared.command.CommandResult;
import bg.btanev.eightyfour.shared.command.profile.FindProfilesCommand;
import bg.btanev.eightyfour.shared.command.profile.FindProfilesInput;
import bg.btanev.eightyfour.shared.dto.TargetDto;
import bg.btanev.eightyfour.shared.dto.profile.ProfileDto;
import bg.btanev.eightyfour.shared.dto.profile.ProfileEventDto;

/**
 *
 * @author B.Tanev
 *
 */
public class FindProfilesCommandHandler
		extends
		GenericCommandHandler<FindProfilesInput, ArrayList<? extends ProfileDto<? extends ProfileEventDto>>, CommandResult<FindProfilesInput, ArrayList<? extends ProfileDto<? extends ProfileEventDto>>>, FindProfilesCommand> {

	private final TargetService targetService;
	private final ProfileService profileService;
	private final DtoConverter converter;

	public FindProfilesCommandHandler(TargetService targetService, ProfileService profileService, DtoConverter converter) {
		super();

		Validate.notNull(targetService);
		Validate.notNull(profileService);
		Validate.notNull(converter);

		this.targetService = targetService;
		this.profileService = profileService;
		this.converter = converter;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected ArrayList<? extends ProfileDto<? extends ProfileEventDto>> doExecuteInternal(
			FindProfilesInput attachedInput) {

		Validate.notNull(attachedInput);

		TargetDto targetInput = attachedInput.getTargetDto();
		Validate.notNull(targetInput);

		Long targetId = targetInput.getId();
		Validate.notNull(targetId);
		Validate.isTrue(targetId.longValue() > 0);

		ArrayList profilesResult = null;
		Target target = targetService.findByIdentifier(targetId);
		if (target != null) {
			List<Profile<ProfileEvent>> profiles = profileService.findAllProfiles(target);
			if (profiles != null && profiles.size() > 0) {
				profilesResult = converter.collectionDomainToDto(profiles, 1);
			}
		}

		return profilesResult;
	}

	@Override
	protected CommandResult<FindProfilesInput, ArrayList<? extends ProfileDto<? extends ProfileEventDto>>> packCommandResult(
			FindProfilesInput input, ArrayList<? extends ProfileDto<? extends ProfileEventDto>> detachedResult) {

		return new CommandResult<FindProfilesInput, ArrayList<? extends ProfileDto<? extends ProfileEventDto>>>(input,
				detachedResult);
	}

	@Override
	public FindProfilesInput attach(FindProfilesInput input) {
		return input;
	}

	@Override
	public ArrayList<? extends ProfileDto<? extends ProfileEventDto>> detach(
			ArrayList<? extends ProfileDto<? extends ProfileEventDto>> result) {

		return result;
	}
}
